package cn.sdfykjyxgs.synclient.server;

import cn.sdfykjyxgs.synclient.base.HttpResult;
import cn.sdfykjyxgs.synclient.config.HttpTool;
import cn.sdfykjyxgs.synclient.entity.FileSynConfig;
import cn.sdfykjyxgs.synclient.mapper.FileSynConfigMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.stream.Collectors;

@Service
public class FileSynConfigServiceImpl extends ServiceImpl<FileSynConfigMapper, FileSynConfig> implements FileSynConfigService {
    @Resource
    HttpTool httpTool;

    public HttpResult synData(FileSynConfig fileSynConfig) {
        HttpResult httpResult = new HttpResult();
        fileSynConfig.setModel(Arrays.stream(fileSynConfig.getRealTimeStatus()).collect(Collectors.joining(",")));
        saveOrUpdate(fileSynConfig);
        httpResult.setData(fileSynConfig);
        HttpTool.initSetting(fileSynConfig);
        httpTool.changeSpeed(fileSynConfig);
        return httpResult;
    }

    @Override
    public HttpResult configPageData() {
        HttpResult httpResult = new HttpResult();
        QueryWrapper<FileSynConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.last(" limit 1");
        FileSynConfig fileSynConfig = baseMapper.selectOne(queryWrapper);
        if (fileSynConfig == null) fileSynConfig = new FileSynConfig();
        String model = fileSynConfig.getModel();
        if (StringUtils.isNotBlank(model)) {
            String[] split = model.split(",");
            fileSynConfig.setRealTimeStatus(split);
        }
        httpResult.setData(fileSynConfig);
        return httpResult;
    }
}
